package com.jiinfo.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jiinfo.controller.ApiController;
import com.jiinfo.controller.DicController;
import com.jiinfo.controller.IndexController;
import com.jiinfo.controller.OrganController;
//import com.jiinfo.controller.PingController;
import com.jiinfo.controller.ResourceController;
import com.jiinfo.controller.RoleController;
import com.jiinfo.controller.UserController;
import com.jiinfo.interceptor.AuthInterceptor;
import com.jiinfo.interceptor.CommonInterceptor;
import com.jiinfo.model.Dic;
import com.jiinfo.model.DicNode;
import com.jiinfo.model.FileInfo;
import com.jiinfo.model.Organ;
import com.jiinfo.model.Resource;
import com.jiinfo.model.Role;
import com.jiinfo.model.RoleResource;
import com.jiinfo.model.SysLog;
import com.jiinfo.model.User;
import com.jiinfo.model.UserRole;

/**
 * API引导式配置
 */
public class ProjectConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setMaxPostSize(PropKit.getInt("file.MaxPostSize"));//
		
		me.setErrorView(401, "/500.html");
		me.setErrorView(403, "/500.html");
		me.setError404View("/404.html");
		me.setError500View("/500.html");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/user", UserController.class);
		me.add("/resource", ResourceController.class);
		me.add("/role", RoleController.class);
		me.add("/organ", OrganController.class);
		me.add("/dic", DicController.class);
		me.add("/api", ApiController.class);
		
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
		me.add(c3p0Plugin);
		
		//定时任务插件
		//QuartzPlugin quartzPlugin=new QuartzPlugin(UserStudyEndJob.class);
		//me.add(quartzPlugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		arp.setShowSql(PropKit.getBoolean("showSql", false));
		me.add(arp);
		arp.addMapping("t_user", User.class);
		arp.addMapping("t_resource", Resource.class);	
		arp.addMapping("t_role", Role.class);
		arp.addMapping("t_role_resource", RoleResource.class);
		arp.addMapping("t_user_role", UserRole.class);
		arp.addMapping("t_dic_node", DicNode.class);
		arp.addMapping("t_dic_node", Dic.class);
		arp.addMapping("t_file_info", FileInfo.class);
		arp.addMapping("t_sys_log", SysLog.class);
		arp.addMapping("t_organ", Organ.class);
		
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new CommonInterceptor());
		me.add(new AuthInterceptor());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		
	}
	
	public void afterJFinalStart(){
		JFinal.me().getServletContext().setAttribute("CDN_HOST", PropKit.get("cdn_host"));
	};
}
